﻿using Common.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Common.Tools
{
    public class Functions
    {
        public static string ObjectToXmlString<T>(T target)
        {
            string str = null;

            StringWriter sw = new StringWriter();
            XmlSerializer serializer_room = new XmlSerializer(typeof(T));
            serializer_room.Serialize(sw, target);
            sw.Close();
            str = sw.ToString();

            return str;
        }

        public static T XmlStringToObject<T>(string str)
        {
            T obj;
            using (StringReader reader = new StringReader(str))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                obj = (T)serializer.Deserialize(reader);
            }

            return obj;
        }
        
        /////////////////////////////////////////////////////////////////////////////////
        public static CommonUser GetCommonUser(List<CommonUser> list, int id)
        {
            CommonUser returnUser = null;
            for (int i = 0; i < list.Count; i++)
            {
                CommonUser user = list[i];
                if (user.id == id)
                {
                    returnUser = user;
                    break;
                }
            }
            
            return returnUser;
        }

        public static Room GetRoom(List<Room> list, int roomId)
        {
            Room returnRoom = null;
            for (int i = 0; i < list.Count; i++)
            {
                Room room = list[i];
                if (room.roomId == roomId)
                {
                    returnRoom = room;
                    break;
                }
            }

            return returnRoom;
        }


        

        // 通过玩家id从房间列表中获取房间
        public static Room GetRoomFromRoomListByPlayerId(List<Room> roomList, int playerId)
        {
            Room returnRoom = null;
            for (int i = 0; i < roomList.Count; i++)
            {
                Room room = roomList[i];
                for (int j = 0; j < room.playerList.Count; j++)
                {
                    CommonUser user = room.playerList[j];
                    if (user.id == playerId)
                    {
                        returnRoom = room;
                        break;
                    }
                }
            }

            return returnRoom;
        }

        //玩家是否在该房间中
        public static bool IsPlayerInRoom(Room room, int playerId)
        {
            bool isInRoom = false;

            for (int i = 0; i < room.playerList.Count; i++)
            {
                CommonUser user = room.playerList[i];
                if (user.id == playerId)
                {
                    isInRoom = true;
                    break;
                }
            }

            return isInRoom;
        }
        public static CommonUser AddPlayerFromRoomList(Room room, CommonUser user)
        {
            bool isInRoom = IsPlayerInRoom(room, user.id);
            if (isInRoom)
            {

            }
            else
            {
                room.playerList.Add(user);
            }

            return user;
        }

        // 将玩家从玩家当前房间移除
        public static Room RemovePlayerFromRoomList(List<Room> roomList, int playerId)
        {
            Room returnRoom = null;
            for (int i = 0; i < roomList.Count; i++)
            {
                Room room = roomList[i];
                for (int j = 0; j < room.playerList.Count; j++)
                {
                    CommonUser user = room.playerList[j];
                    if (user.id == playerId)
                    {
                        room.playerList.RemoveAt(j);
                        returnRoom = room;
                        break;
                    }
                }
            }

            return returnRoom;
        }

        // 获取玩家在房间的第几个
        public static int GetAvatarIndex(List<CommonUser> playerList, int playerId)
        {
            int avatarIndex = -1;
            for (int i = 0; i < playerList.Count; i++)
            {
                CommonUser user = playerList[i];
                if (user.id == playerId)
                {
                    avatarIndex = i;
                    break;
                }
            }

            return avatarIndex;
        }

        // 获取玩家在房间的第几个的CommonUser
        public static CommonUser GetAvatarUser(List<CommonUser> playerList, int playerId)
        {
            CommonUser user = null;
            int avatarIndex = GetAvatarIndex(playerList, playerId);
            if (avatarIndex != -1)
            {
                user = playerList[avatarIndex];
            }

            return user;
        }

        //// 通过玩家id从指定房间的玩家列表中获取玩家的位置
        //public static int GetIndexFromPlayerListByPlayerId(List<CommonUser> playerList, int playerId)
        //{
        //    int index = 0;
        //    for (int i = 0; i < playerList.Count; i++)
        //    {
        //        CommonUser user = playerList[i];
        //        if (user.id == playerId)
        //        {
        //            index = i;
        //            break;
        //        }
        //    }

        //    return index;
        //}
    }
}
